<?php

class Communitas_Integrate_Model_Observer {

	public function checkoutOnepageControllerSuccessAction($observer) {
		$onepage = Mage::getSingleton('checkout/type_onepage');
		$checkout = Mage::getSingleton('checkout/session');

		$lastQuoteId = $onepage->getCheckout()->getLastQuoteId();
        $lastOrderId = $onepage->getCheckout()->getLastOrderId();
		$lastRealOrderId = $onepage->getCheckout()->getLastRealOrderId();

		$order = Mage::getModel('sales/order')->load( $lastOrderId );

		$vars = parse_ini_file($_SERVER['DOCUMENT_ROOT']."/app/code/local/Communitas/Integrate/etc/variables.ini", true);
		$baseUrl = $vars['communitas_homepage_url'];
		$locale = strtolower(str_replace(array('_',' '),'-', Mage::app()->getLocale()->getDefaultLocale()));
		$successUrl = 'commerce/checkout/success';
		$redirect = $baseUrl.'/'.$locale.'/'.$successUrl;

		$redirect .= '/order/'.$lastRealOrderId;
		$redirect .= '/auth/'.md5( $order->getCreatedAt().'_'.$order->getCustomerEmail() );
		$redirect .= '/';

		header('Location: '.$redirect);
		exit;
	}

	public function clearCommunitasCacheByTags(Varien_Event_Observer $observer) {
		// TODO - THIS DOESN'T GET HIT AT ALL - WHY???
// file_put_contents('/home/communitas/workspace/bd_magento/var/tmp/observer_debug.txt', 'oh yes it does');
		//         $category = $observer->getEvent()->getCategory();
		// $vars = parse_ini_file($_SERVER['DOCUMENT_ROOT']."/app/code/local/Communitas/Integrate/etc/variables.ini", true);
		// $post_url = $vars['communitas_cache_clean_post_url'];

		// TODO add CURL code to post to Communitas here

        // return $this;
	}

}
